package com.yewen.websocket.handler;

import com.yewen.util.HMACEncryptUtil;
import com.yewen.websocket.message.ForwardToOneMessage;
import com.yewen.websocket.message.SendToOneMessage;
import com.yewen.websocket.util.WebSocketUtil;
import org.springframework.stereotype.Component;

import javax.websocket.Session;

/**
 * @Classname SendToOneHandler
 * @Description 正常发送消息的处理器
 * @Date 2022/3/25 16:03
 * @Created by TaoVh
 */
@Component
public class SendToOneHandler implements MessageHandler<SendToOneMessage>{

    @Override
    public void execute(Session session, SendToOneMessage message) {
        /*Token验证*/
//        if(!HMACEncryptUtil.verify(message.getToken())){
//            /*返回告知用户验证不通过*/
//            return;
//        }
        /**
         * 这里假设双方都没有掉线等异常，发送的信息都能准确到达对方。ACK可解决此问题
         */
//        System.out.println("[SendToOneHandler]"+message.toString());

        String toUser = message.getToUser();
        ForwardToOneMessage forwardToOneMessage = new ForwardToOneMessage();
        forwardToOneMessage.setContent(message.getContent());
        forwardToOneMessage.setMsgID(message.getMsgID());
        forwardToOneMessage.setFromUser(message.getFromUser());
        WebSocketUtil.send(toUser,ForwardToOneMessage.TYPE,forwardToOneMessage);
    }

    @Override
    public String getType() {
        return SendToOneMessage.TYPE;
    }
}
